﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.IO;

namespace FlyPublisher
{
    class Program
    {
        static void Main(string[] args)
        {
            String msg = "{\"Channel\":\"1111\",\"MessageData\":\"Sample Message from publisher at " + DateTime.Now.ToString("g") + "\"}";

            PushMessageToServer("http://localhost:49999/FlyCometHandler.ashx", String.Format("command={0}&message={1}", HttpUtility.UrlEncode("post"), HttpUtility.UrlEncode(msg)));
        }

        private static void PushMessageToServer(string strPage, string strBuffer)
        {
            //Our postvars
            byte[] buffer = Encoding.ASCII.GetBytes(strBuffer);
            //Initialization
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(strPage);
            //Our method is post, otherwise the buffer (postvars) would be useless
            WebReq.Method = "POST";
            //We use form contentType, for the postvars.
            WebReq.ContentType = "application/x-www-form-urlencoded";
            //The length of the buffer (postvars) is used as contentlength.
            WebReq.ContentLength = buffer.Length;
            //We open a stream for writing the postvars
            Stream PostData = WebReq.GetRequestStream();
            //Now we write, and afterwards, we close. Closing is always important!
            PostData.Write(buffer, 0, buffer.Length);
            PostData.Close();
            //Get the response handle, we have no true response yet!
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            //Let's show some information about the response
            Console.WriteLine(WebResp.StatusCode);
            Console.WriteLine(WebResp.Server);

            //Now, we read the response (the string), and output it.
            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            Console.WriteLine(_Answer.ReadToEnd());
        }   

    }
}
